# -*- coding: utf-8 -*-
# @Time    : 2022/3/21 14:14
# @Author  : Zhy
# @Email   : mat_wu@163.com
# @File    : exports.py
# @Software: PyCharm

from flask_restful import Resource, Api, marshal, marshal_with, reqparse
from flask import Blueprint
from common.models.user import *
from common.utils.jwt_token import create_token

stu_bp = Blueprint('stu_bp', __name__)

api = Api(stu_bp)


class Login(Resource):
    """
    用户登录
    """

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('account')
        parser.add_argument('password')
        args = parser.parse_args()
        account = args.get('account')
        password = args.get('password')
        user = User.query.filter_by(account=account, password=password).first()
        if user:
            user_id = user.id
            token, refresh_token = create_token(user_id)
            db.session.commit()
            return {'code': 200, 'message': 'ok', 'token': token, 'refresh_token': refresh_token}
        else:
            return {'code': 500, 'message': '用户不存在'}


api.add_resource(Login, '/login', endpoint='login')
